
var provinceArray = new Array();
var provinceTag = document.getElementById("province");
var num = 20


$(document).ready(function () {
    getStart()
    getData(p)
    for(var i=0; i<provinceList.length; i++){
        var province = provinceList[i];
        var provinceName = ''
        if(province.name  == '北京'||province.name  == '上海'||province.name  == '天津'||province.name  == '重庆'){
            provinceName = province.name + '市'
        }else{
            provinceName = province.name + '省'
        }
        provinceArray[i] = provinceName;
        provinceTag.add(new Option(provinceName,i));
    }
});

function getStart(){
    $.ajax({
        url: global.apiUrl + '/admin_yes_no',
        type: 'GET',
        dataType: 'json',
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(res)
            switch (res.content) {
                case 'yes':
                    $("input[type='radio']:eq(0)").attr("checked",'checked');
                    break;
                case 'no':
                    $("input[type='radio']:eq(1)").attr("checked",'checked');
                    break;
            }
        },
    })
}

function getData(p){
    let time = '00:00:00'
    startTime = $('#start').val() != ''? $('#start').val() + ' ' + time:'2019-01-01' + ' ' + time;//开始时间
    endTime = $('#end').val() != ''? $('#end').val() + ' ' + '23:59:59': laydate.now(1, 'YYYY-MM-DD') + ' ' + time;//结束时间
    $.ajax({
        url: global.apiUrl + '/get_user_gy',
        type: 'GET',
        dataType: 'json',
        data: {
            p: p,
            start_time: startTime,
            end_time: endTime,
            num: num,
            sheng: $("#province option:selected").text() != '全部'?$("#province option:selected").text(): ''
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            console.log(res)
            var html = ''
            for(var i in res.content){
                html += `<tr>
                            <td>
                                <div class="userItem">
                                    <text>${res.content[i].content.nickname}</text>
                                    <text>${res.content[i].content.sex} ${res.content[i].content.height}cm ${res.content[i].content.startweight}kg</text>
                                    <text style="margin-bottom:0">ID: ${res.content[i].user_id}</text>
                                </div>
                            </td>
                            <td>${res.content[i].name}</td>
                            <td>${jsGetAge(res.content[i].content.birthday)}</td>
                            <td>${res.content[i].phone}</td>
                            <td>${res.content[i].wechat?res.content[i].wechat:'-'}</td>
                            <td>${res.content[i].add_time}</td>
                            <td>${res.content[i].region}</td>
                            <td>${res.content[i].address}</td>
                            <td>
                                <div class="userBtn">
                                    ${audit(res.content[i].status,res.content[i].id)}
                                </div>
                            </td>
                        </tr>`
            }
            $('#userList').html(html)
            $("#page").paging({
                pageNo: p,
                totalPage: Math.ceil(res.count/20),
                totalSize: res.count,
                callback: function(num) {
                    // console.log(num)
                    getData(num)
                }
            })
        }
    })

    
}

function clickBtn(){
    getData(p)
}

function clickBtn2(){
    $.ajax({
        url: global.apiUrl + '/save_yes_no',
        type: 'GET',
        dataType: 'json',
        data: {
            content: $("input[type='radio']:checked").val()
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            if(res == 1){
                alert('活动提交成功')
            }else{
                alert('活动提交失败')
            }
        }
    })
}

function audit(status,id){
    var html = ''
    switch (status) {
        case '0':
            html = `<button class="btn btn-white btn-sm through" style="margin-right: 15px" data-id="${id}">中奖</button>
                     <button class="btn btn-white btn-sm unThrough" data-id="${id}">未中奖</button>`
            break;
        case '1':
            html = `<button disabled class="btn btn-white btn-sm through" data-id="${id}">中奖</button>`
            break;
        case '2':
            html = `<button disabled class="btn btn-white btn-sm unThrough" data-id="${id}">未中奖</button>`
            break;
    }
    return html
}

$("#userList").on('click',".through",function(){
    var id = $(this).data('id')
    through(id)
})

$("#userList").on('click',".unThrough",function(){
    var id = $(this).data('id')
    unThrough(id)
})

function through(id){
    $.ajax({
        url: global.apiUrl + '/zhong',
        type: 'GET',
        dataType: 'json',
        data: {
            id: id
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            if(res == 1){
                // alert('中奖审核成功')
                getData(p)
            }else{
                alert('中奖审核失败')
            }
        }
    })
}

function unThrough(id){
    $.ajax({
        url: global.apiUrl + '/no_zhong',
        type: 'GET',
        dataType: 'json',
        data: {
            id: id
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            if(res == 1){
                // alert('未中奖审核成功')
                getData(p)
            }else{
                alert('未中奖审核失败')
            }
        }
    })
}

function execl(){
    num = 100
    let time = '00:00:00'
    startTime = $('#start').val() != ''? $('#start').val() + ' ' + time:'2019-01-01' + ' ' + time;//开始时间
    endTime = $('#end').val() != ''? $('#end').val() + ' ' + '23:59:59': laydate.now(1, 'YYYY-MM-DD') + ' ' + time;//结束时间
    $.ajax({
        url: global.apiUrl + '/get_user_gy',
        type: 'GET',
        dataType: 'json',
        data: {
            p: p,
            start_time: startTime,
            end_time: endTime,
            num: num,
            sheng: $("#province option:selected").text() != '全部'?$("#province option:selected").text(): ''
        },
        header: {
            'content-type': 'application/json' //默认值
        },
        success: function(res){
            // console.log(res)
            var letter = ['A','B','C','D','E','F','G','H','I','J'];
            var tableheader = ['姓名','年龄','身高','体重','性别','电话','微信号','时间','地址','详细地址'];
            var data = [];
            var list = res.content;
            for(var i in list){
                var arr =[]
                arr.push(list[i].name,jsGetAge(list[i].content.birthday),list[i].content.height,list[i].content.startweight,list[i].content.sex,list[i].phone,list[i].wechat?list[i].wechat:'-',list[i].add_time,list[i].region,list[i].address);
                data.push(arr);
            }
            console.log(data)
            var text = '公益减重'
            $.ajax({
                url: global.apiUrl + '/Api/Excel/ce',
                type: 'get',
                data: {
                    letter: letter,
                    tableheader: tableheader,
                    data: data,
                    pro: text,
                },
                success: function(res){
                    // console.log(res)

                    window.location.href = global.apiUrl+'/add_excel?id=' + res
                },
            })
        }
    })
    
}